package gui;

import entryPoint.LoginInterface;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.io.ObjectInputStream;
import java.io.PrintStream;
import java.net.Socket;

/**
 *
 * @author Raman
 */
public class RegisterInterface extends JFrame implements ActionListener {

  private JLabel userNameLabel, passWordLabel, confirmPasswordLabel,
          empIDLabel, firstNameLabel, lastNameLabel, ageLabel, sexLabel;
  private JTextField userNameField, empIDField, firstNameField, lastNameField, ageField;
  private JPasswordField passwordField, confirmPasswordField;
  private JComboBox genderCombo;
  private JButton submitButton, cancelButton;
//    UserInfo----------------------------------------------------------------
  private String userName;
  private String passWord;
  private String confPassword;
  private String empID;
  private String firstName;
  private String lastName;
  private int age;
  private String sex;
//    Header or Footer---------------------------------------------------------
  private JPanel mainPanel;
  private JLabel mainHeading, status;
  //Combobox----------------------------------------------------------------
  private String[] items = {"male", "female"};
  //Layout Stuff
  GridBagLayout gbl;
  GridBagConstraints gbc;
  //Networking Stuff
  Socket toServer;
  ObjectInputStream streamFromServer;
  PrintStream streamToServer;

  public RegisterInterface() {
    super("Register Interface");
    initComponents();

    mainHeading = new JLabel("Register Interface");
    mainHeading.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    mainHeading.setFont(new Font("Times New Roman", Font.BOLD, 30));
    add(mainHeading, BorderLayout.NORTH);

    add(mainPanel, BorderLayout.CENTER);

    status = new JLabel("Welcome Guest");
    status.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    status.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    status.setForeground(Color.RED);
    add(status, BorderLayout.SOUTH);

  } //END of CONSTRUCTOR

// actionPerformed-------------------------------------------------------------
  @Override
  public void actionPerformed(ActionEvent event) {

    if (event.getActionCommand().equals("cancel")) {

      this.dispose(); //close the register window

    } else if (event.getActionCommand().equals("submit")) {

      if (validateData()) {
        try {
          toServer = new Socket("127.0.0.1", 4000);
          streamFromServer = new ObjectInputStream(toServer.getInputStream());
          streamToServer = new PrintStream(toServer.getOutputStream());

          //Send user data to Server 
          userName = userNameField.getText();
          String strUsrPwd = new String(passwordField.getPassword());
          passWord = strUsrPwd;

          //send the userName and passWord 
          streamToServer.println("RegisterInfo");
          streamToServer.println(userName + ":" + passWord);

          //read the response from the server
          String msgFromServer = (String) streamFromServer.readObject();

          if (msgFromServer.equals("Registered")) {

            this.dispose(); //close the register window
            (new LoginInterface()).setStatus("Registration Successfull");//open login window

          } else if (msgFromServer.equals("User Exists")) {
            status.setText("User Exists");
          }
        } //END of try
        catch (Exception ex) {
          status.setText(ex.getMessage());
          System.out.println(ex.getCause());
        }
      }//END of if
    }//END of else if
  } //END of actionPerformed

//initComponents---------------------------------------------------------------
  private void initComponents() {
    gbl = new GridBagLayout();
    gbc = new GridBagConstraints();

    Insets insets = new Insets(5, 5, 5, 5);
    gbc.insets = insets;

    mainPanel = new JPanel();
    mainPanel.setLayout(gbl);
    getContentPane().add(mainPanel);

    //----------------------------------------------------------------------   
    gbc.gridx = 0;
    gbc.gridy = 0;
    userNameLabel = new JLabel("Enter Username");
    mainPanel.add(userNameLabel, gbc);
    //----------------------------------------------------------------------   
    gbc.gridx = 1;
    gbc.gridy = 0;
    userNameField = new JTextField(20);
    mainPanel.add(userNameField, gbc);
    //----------------------------------------------------------------------   
    gbc.gridx = 0;
    gbc.gridy = 1;
    passWordLabel = new JLabel("Enter Password");
    mainPanel.add(passWordLabel, gbc);
    //----------------------------------------------------------------------   
    gbc.gridx = 1;
    gbc.gridy = 1;
    passwordField = new JPasswordField(20);
    mainPanel.add(passwordField, gbc);
    //----------------------------------------------------------------------   
    gbc.gridx = 0;
    gbc.gridy = 2;
    confirmPasswordLabel = new JLabel("Confirm Password");
    mainPanel.add(confirmPasswordLabel, gbc);
    //----------------------------------------------------------------------   
    gbc.gridx = 1;
    gbc.gridy = 2;
    confirmPasswordField = new JPasswordField(20);
    mainPanel.add(confirmPasswordField, gbc);
    //----------------------------------------------------------------------
    gbc.gridx = 0;
    gbc.gridy = 3;
    empIDLabel = new JLabel("Employee ID");
    mainPanel.add(empIDLabel, gbc);
    //----------------------------------------------------------------------
    gbc.gridx = 1;
    gbc.gridy = 3;
    empIDField = new JTextField(20);
    mainPanel.add(empIDField, gbc);
    //----------------------------------------------------------------------
    gbc.gridx = 0;
    gbc.gridy = 4;
    firstNameLabel = new JLabel("First Name");
    mainPanel.add(firstNameLabel, gbc);
    //----------------------------------------------------------------------   
    gbc.gridx = 1;
    gbc.gridy = 4;
    firstNameField = new JTextField(20);
    mainPanel.add(firstNameField, gbc);
    //----------------------------------------------------------------------
    gbc.gridx = 0;
    gbc.gridy = 5;
    lastNameLabel = new JLabel("Last Name");
    mainPanel.add(lastNameLabel, gbc);
    //----------------------------------------------------------------------
    gbc.gridx = 1;
    gbc.gridy = 5;
    lastNameField = new JTextField(20);
    mainPanel.add(lastNameField, gbc);
    //----------------------------------------------------------------------
    gbc.gridx = 0;
    gbc.gridy = 6;
    ageLabel = new JLabel("Age");
    mainPanel.add(ageLabel, gbc);
    //----------------------------------------------------------------------
    gbc.gridx = 1;
    gbc.gridy = 6;
    ageField = new JTextField(20);
    mainPanel.add(ageField, gbc);
    //----------------------------------------------------------------------
    gbc.gridx = 0;
    gbc.gridy = 7;
    sexLabel = new JLabel("Sex");
    mainPanel.add(sexLabel, gbc);
    //----------------------------------------------------------------------
    gbc.gridx = 1;
    gbc.gridy = 7;
    genderCombo = new JComboBox(items);
    mainPanel.add(genderCombo, gbc);

    //----------------------------------------------------------------------   
    gbc.gridx = 1;
    gbc.gridy = 8;
    submitButton = new JButton("Submit");
    submitButton.setMnemonic(KeyEvent.VK_S);
    mainPanel.add(submitButton, gbc);
    submitButton.setActionCommand("submit");
    submitButton.addActionListener(this);

    //---------------------------------------------------------------------- 
    gbc.gridx = 2;
    gbc.gridy = 8;
    cancelButton = new JButton("Cancel");
    cancelButton.setMnemonic(KeyEvent.VK_C);
    mainPanel.add(cancelButton, gbc);
    cancelButton.setActionCommand("cancel");
    cancelButton.addActionListener(this);

    //frame related stuff
    setVisible(true);
    setSize(new Dimension(500, 400));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  }   //END of INIT-COMPONENTS

  /**Used to validate the data*/
  private boolean validateData() {
    String strUsrPwd = new String(passwordField.getPassword());
    String strCnfPwd = new String(confirmPasswordField.getPassword());

    userName = userNameField.getText();
    passWord = strUsrPwd;
    confPassword = strCnfPwd;
    firstName = firstNameField.getText();
    lastName = lastNameField.getText();
    empID = empIDField.getText();
    try {
      age = Integer.parseInt(ageField.getText());
    } catch (Exception ex) {
      status.setText(ex.getMessage());
    }

    if (userName.length() < 0) {

      status.setText("Invalid userName");

    } else if ((strUsrPwd.length() < 0)
            || (strCnfPwd.length() < 0)
            || !(strUsrPwd.equals(strCnfPwd))) {

      status.setText("Invalid Passwords");

    } else if (firstName.length() < 0) {

      status.setText("Invalid firstName");

    } else if (lastName.length() < 0) {

      status.setText("Invalid lastName");

    } else if (empID.length() < 0) {

      status.setText("Invalid empID");

    } else if (!(age > 21 && age < 58)) {

      status.setText("Invalid age");

    } else {

      status.setText("Data Verified");
      return true;

    }
    return false;
  }
}
